package com.ryx.controller;

import com.github.pagehelper.Page;
import com.ryx.constants.Constant;
import com.ryx.entity.SysRotationChart;
import com.ryx.service.RotationChartService;
import com.ryx.utils.DataResult;
import com.ryx.utils.JWTTokenUtil;
import com.ryx.vo.req.RotationChartDeleteReqVo;
import com.ryx.vo.req.RotationChartReqAddVo;
import com.ryx.vo.req.RotationChartUpdateReqVo;
import com.ryx.vo.req.RotationPageReqVo;
import com.ryx.vo.resp.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @BelongsPackage: com.ryx.controller
 * @Author: 容永轩
 * @CreateTime: 2021-01-28
 * @Description:
 */
@RestController
@RequestMapping("/api")
@Api(tags = "轮播图功能相关接口")
public class RotationController {

    @Autowired
    private RotationChartService rotationChartService;


    @PostMapping("/rotations")
    @ApiOperation(value = "分页获取轮播信息接口")
    @RequiresPermissions("sys:rotation:list")
    public DataResult<PageVo<SysRotationChart>> pageInfo(@RequestBody RotationPageReqVo vo){
        DataResult<PageVo<SysRotationChart>> result =DataResult.success();

        result.setData(rotationChartService.pageInfo(vo));

        return  result;
    }

    @GetMapping("/rotations")
    @ApiOperation(value = "轮播图展示接口")
    public DataResult<List<SysRotationChart>> selectAll(){
        DataResult result= DataResult.success();
        List<SysRotationChart> sysRotationCharts = rotationChartService.selectAll();
        result.setData(sysRotationCharts);
        return result;
    }

    @PostMapping("/rotation")
    @ApiOperation(value = "轮播图新增接口")
    @RequiresPermissions("sys:rotation:add")
    public DataResult addRotation(@RequestBody RotationChartReqAddVo vo , HttpServletRequest request){
        String accessToken = request.getHeader(Constant.ACCESS_TOKEN);
        String userId = JWTTokenUtil.getUserId(accessToken);

        rotationChartService.addRotation(vo,userId);

        return DataResult.success();

    }

    @PutMapping("/rotation")
    @ApiOperation(value = "编辑更新轮播图")
    @RequiresPermissions("sys:rotation:update")
    public DataResult updateRotation(@RequestBody RotationChartUpdateReqVo vo ,HttpServletRequest request){

        String accessToken = request.getHeader(Constant.ACCESS_TOKEN);
        String userId = JWTTokenUtil.getUserId(accessToken);

        rotationChartService.updateRotationInfo(vo,userId);

        return DataResult.success();
    }


    @DeleteMapping("/rotation")
    @ApiOperation(value = "删除轮播图接口")
    @RequiresPermissions("sys:rotation:delete")
    public DataResult  deleteRotation(@RequestBody @Valid List<RotationChartDeleteReqVo> list){
        rotationChartService.batchDeleteRotation(list);

        return DataResult.success();
    }


}
